package com.xmb.wechat.view.wechat.setting;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.ACache;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatContactBean_;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.definterface.PicChooseWithCutCallBack;
import com.xmb.wechat.entity.WechatAppInfoBean;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.util.PicLoadUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatSettingActivity extends BaseActivity {

    @BindView(R2.id.iv_back)
    ImageView      ivBack;
    @BindView(R2.id.iv_avatar)
    ImageView      ivAvatar;
    @BindView(R2.id.btn_pick_avatar)
    RelativeLayout btnPickAvatar;
    @BindView(R2.id.iv_arrow)
    ImageView      ivArrow;
    @BindView(R2.id.tv_name)
    TextView       tvName;
    @BindView(R2.id.btn_clean_all_chat)
    RelativeLayout btnCleanAllChat;
    @BindView(R2.id.tv_id)
    TextView       tvId;
    @BindView(R2.id.btn_delete_all_contact)
    RelativeLayout btnDeleteAllContact;

    private WechatAppInfoBean vo;

    public WechatSettingActivity() {
        super(R.layout.wechat_activity_my_info_setting);
    }

    @Override
    protected void initView() {
        vo = WechatAppInfoBean.getWechat(this);
        if (vo.getChatCommonBG() == null) {
            Picasso.get().load(R.drawable.img_default_gray).into(ivAvatar);
        } else {
            PicLoadUtils.loadSdcardPic(vo.getChatCommonBG(),ivAvatar);
        }
    }

    private void pickAvatar() {
        new AlertDialog.Builder(this).setItems(new String[]{"选择背景图片", "清除背景图片（使用默认背景）"},
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            PicChooseUtils.chooseOnePicWithCut(WechatSettingActivity.this, new PicChooseWithCutCallBack() {
                                @Override
                                public void onPicChoose(String... Pic) {
                                    if (TextUtils.isEmpty(Pic[0]))
                                        return;
                                    vo.setChatCommonBG(Pic[0]);
                                    ACache.get(WechatSettingActivity.this).put("WechatAppInfoBean", vo);
                                    PicLoadUtils.loadSdcardPic(Pic[0],ivAvatar);
                                }
                            });
                        } else if (which == 1) {
                            vo.setChatCommonBG(null);
                            ACache.get(WechatSettingActivity.this).put("WechatAppInfoBean", vo);
                            Picasso.get().load(R.drawable.img_default_gray).into(ivAvatar);
                        }
                    }
                }).show();
    }

    @OnClick({R2.id.iv_back, R2.id.btn_pick_avatar, R2.id.btn_change_font_size, R2.id.btn_clean_all_chat,
            R2.id.btn_delete_all_contact})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.iv_back) {
            finish();
        } else if (i == R.id.btn_pick_avatar) {
            pickAvatar();
        } else if (i == R.id.btn_change_font_size) {
            startActivity(new Intent(WechatSettingActivity.this, WeChatChangeFontSizeActivity.class));
        } else if (i == R.id.btn_clean_all_chat) {
            cleanAllChat();
        } else if (i == R.id.btn_delete_all_contact) {
            deleteAllContact();
        }
    }

    private void cleanAllChat() {
        new AlertDialog.Builder(this).setMessage("确定清空所有聊天记录？").setPositiveButton("确定",
                (dialog, which) -> {
                    WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).removeAll();
                    WechatStartApplication.getBoxStore().boxFor(WechatLastMsgBean.class).removeAll();
                    ToastUtils.showLong("清空聊天记录成功");
                }).setNegativeButton("取消", null).show();
    }

    private void deleteAllContact() {
        new AlertDialog.Builder(this).setMessage("确定删除所有微信好友？聊天记录将会一同删除").setPositiveButton("确定",
                (dialog, which) -> {
                    WechatStartApplication.getBoxStore().boxFor(WechatMsgBean.class).removeAll();
                    WechatStartApplication.getBoxStore().boxFor(WechatLastMsgBean.class).removeAll();
                    WechatStartApplication.getBoxStore().boxFor(WechatContactBean.class).query()
                            .equal(WechatContactBean_.isInner, false).build().remove();
                    ToastUtils.showLong("删除微信好友成功");
                }).setNegativeButton("取消", null).show();
    }
}
